import { _decorator, Component, director, Node, Prefab, resources, Sprite, SpriteFrame } from 'cc';
import { UILayerMgr } from './utils/UILayerMgr';
import { PoolMgr } from './utils/PoolMgr';
import { convertNumber } from './utils/utils';
import { UserInfo } from './UserInfo';
import { AudioMgr } from './utils/AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('Home')
export class Home extends Component {
    @property(Prefab)
    greenBall: Prefab;
    @property(Prefab)
    greenBall1: Prefab;
    @property(Node)
    setPanel:Node;

    protected onLoad(): void {
        PoolMgr.getIns().initAllPool("greenBall", this.greenBall, 600);
        PoolMgr.getIns().initAllPool("greenBall1", this.greenBall1, 100);

        
    }

    protected start(): void {
        if (UserInfo.isAudioBgm) {
            AudioMgr.ins.play("audio/bgm");
        }
        else{
            AudioMgr.ins.stop();
        }
    }

    onclickGameStartBtn() {
        AudioMgr.ins.playOneShot("audio/btn",0.7);

        director.loadScene("loadDycxy");
    }
    onclickGameStartBtn2() {
        AudioMgr.ins.playOneShot("audio/btn",0.7);

        director.loadScene("shj");
    }
    onclickSetBtn(){
        AudioMgr.ins.playOneShot("audio/btn",0.7);

        this.setPanel.active=true;
        this.renewAudioUI();
    }

    onclickSetPanelClose(){
        this.setPanel.active=false;
    }

    onclickSetPanelaudioEffectBtn(){
        if (UserInfo.isAudioEffect) {
        UserInfo.isAudioEffect=false;
        }
        else{
        UserInfo.isAudioEffect=true;
        }

        this.renewAudioUI();
    }
    onclickSetPanelBgmBtn(){
        if (UserInfo.isAudioBgm) {
            UserInfo.isAudioBgm=false;
            AudioMgr.ins.stop();
        }
        else{
            UserInfo.isAudioBgm=true;
            AudioMgr.ins.play("audio/bgm");
        }
        this.renewAudioUI();

    }


    renewAudioUI() {
        if (UserInfo.isAudioBgm) {
            resources.load("audioImg/starOn/spriteFrame", SpriteFrame, (err, res) => {
                this.setPanel.getChildByName("bgmBtn").getChildByName("star").getComponent(Sprite).spriteFrame = res;

            })
            resources.load("audioImg/itemBgOn/spriteFrame", SpriteFrame, (err, res) => {
                this.setPanel.getChildByName("bgmBtn").getChildByName("bg").getComponent(Sprite).spriteFrame = res;

            })
            this.setPanel.getChildByName("bgmBtn").getChildByName("star").setPosition(50,0);
        }
        else {
            resources.load("audioImg/starOff/spriteFrame", SpriteFrame, (err, res) => {
                this.setPanel.getChildByName("bgmBtn").getChildByName("star").getComponent(Sprite).spriteFrame = res;

            })
            resources.load("audioImg/itemBgOff/spriteFrame", SpriteFrame, (err, res) => {
                this.setPanel.getChildByName("bgmBtn").getChildByName("bg").getComponent(Sprite).spriteFrame = res;

            })
            this.setPanel.getChildByName("bgmBtn").getChildByName("star").setPosition(-50,0);
         
        }

        if (UserInfo.isAudioEffect) {
            resources.load("audioImg/starOn/spriteFrame", SpriteFrame, (err, res) => {
                this.setPanel.getChildByName("effectBtn").getChildByName("star").getComponent(Sprite).spriteFrame = res;

            })
            resources.load("audioImg/itemBgOn/spriteFrame", SpriteFrame, (err, res) => {
                this.setPanel.getChildByName("effectBtn").getChildByName("bg").getComponent(Sprite).spriteFrame = res;

            })
            this.setPanel.getChildByName("effectBtn").getChildByName("star").setPosition(50,0);

        }
        else {
            resources.load("audioImg/starOff/spriteFrame", SpriteFrame, (err, res) => {
                this.setPanel.getChildByName("effectBtn").getChildByName("star").getComponent(Sprite).spriteFrame = res;

            })
            resources.load("audioImg/itemBgOff/spriteFrame", SpriteFrame, (err, res) => {
                this.setPanel.getChildByName("effectBtn").getChildByName("bg").getComponent(Sprite).spriteFrame = res;

            })
            this.setPanel.getChildByName("effectBtn").getChildByName("star").setPosition(-50,0);

        }
    }

}


